// 导入 模块 
import {ref, reactive } from 'vue'
// 获取后台访问的api
import Api from "../api";

// 定义了一个类 
class ruleData {
  public isEditValue : boolean;
  public passwordValue: string;
  public repasswordValue: string;

  constructor(isEditValue:boolean, passwordValue:string, repasswordValue: string){
    this.isEditValue = isEditValue;
    this.passwordValue = passwordValue;
    this.repasswordValue = repasswordValue;
  }
}

// 实例化一个对象！
var objData = new ruleData(false, "", "")

// 校验员工编号是否存在
const validateSNoExists = (rule: any, value: any, callback: any) =>{
    // 如果是修改，忽略校验
    if(objData.isEditValue) callback();
    // 连接Student接口
    Api.Staff.getAll({id:value,page:1,size:15}).then((res)=>{
      // 判断是否存在
      if(res.data.count>0){
         callback(new Error("员工编号已存在！"))
      } else {
         callback()
      }
    })
}

// 判断两次密码是否一致 
const validatePass2 = (rule: any, value: any, callback: any) => {
  console.log(value);
  console.log( objData.passwordValue);
  
  if (objData.passwordValue === objData.repasswordValue) callback()
  if (value !== objData.passwordValue) {
    callback(new Error("两次密码不一致!"))
  } else {
    callback()
  }
}

var rulesDetail = reactive({
    id: [
      { required: true, message: "员工编号不能为空", trigger: "blur" },
      {
        pattern: /^[9][5]\d{4}$/,
        message: "员工编号必须要是95开头的6位数字",
        trigger: "blur",
      },
      {validator: validateSNoExists, trigger: 'blur' }
    ],
    name: [
      { required: true, message: "姓名不能为空", trigger: "blur" },
      {
        pattern: /^[\u4e00-\u9fa5]{2,5}$/,
        message: "姓名需要2-5个汉字",
        trigger: "blur",
      },
    ],
    pwd: [
      { required: true, message: "密码不能为空", trigger: "blur" },
      { min: 6, message: '密码不能少于6位', trigger: 'blur' },
    ],
    confirmPwd: [
      { required: true, message: "密码不能为空", trigger: "blur" },
      { validator: validatePass2, trigger: 'blur' }
    ],
    position: [{ required: true, message: "职位信息不能为空", trigger: "blur" }],
    placecode: [{ required: true, message: "场地信息不能为空", trigger: "blur" }],
    mobile: [
      { required: true, message: "手机号码不能为空", trigger: "blur" },
      {
        pattern: /^[1][3456789]\d{9}$/,
        message: "手机号码必须要符合规范",
        trigger: "blur",
      },
    ],
    email: [
      { required: true, message: "邮箱地址不能为空", trigger: "blur" },
      {
        pattern: /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/,
        message: "邮箱地址必须要符合规范",
        trigger: "blur",
      },
    ],

})


 export default { objData, rulesDetail}
